{% extends "base.html" %}

{% block title %}上传图片{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-body">
                <h2 class="card-title text-center mb-4">苹果叶片病害诊断</h2>
                <form action="/upload" method="post" enctype="multipart/form-data" class="needs-validation" novalidate>
                    <div class="mb-4">
                        <label for="file" class="form-label">选择图片</label>
                        <input type="file" class="form-control" id="file" name="file" accept="image/*" required>
                        <div class="form-text">支持jpg、jpeg、png格式，建议图片大小不超过5MB</div>
                    </div>
                    <div class="text-center">
                        <button type="submit" class="btn btn-primary btn-lg">开始诊断</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="card mt-4">
            <div class="card-body">
                <h5 class="card-title">使用说明</h5>
                <ol class="list-group list-group-numbered">
                    <li class="list-group-item">点击"选择图片"按钮，选择要诊断的苹果叶片图片</li>
                    <li class="list-group-item">确保图片清晰，叶片特征明显</li>
                    <li class="list-group-item">点击"开始诊断"按钮，等待诊断结果</li>
                    <li class="list-group-item">系统将显示诊断结果和防治建议</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<div class="preview">
    <h2>图片预览</h2>
    <img id="image-preview" src="" alt="图片预览">
    <p id="file-info"></p>
</div>
{% if predictions %}
<div class="row justify-content-center mt-4">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header text-white bg-primary">
                <h4 class="mb-0">诊断结果</h4>
            </div>
            <div class="card-body">
                {% set top_prediction = predictions[0] %}
                <h5 class="card-title">最可能的结果: {{ top_prediction.class_name }}</h5>
                <div class="progress mb-2" style="height: 25px;">
                    <div class="progress-bar" role="progressbar" style="width: {{ top_prediction.probability * 100 }}%;" aria-valuenow="{{ top_prediction.probability * 100 }}" aria-valuemin="0" aria-valuemax="100">
                        <strong>{{ "%.2f"|format(top_prediction.probability * 100) }}%</strong>
                    </div>
                </div>
                <p class="card-text"><strong>防治建议:</strong> {{ top_prediction.suggestion }}</p>
                {% if additional_message %}
                <div class="alert alert-warning mt-3" role="alert">
                    <strong>注意:</strong> {{ additional_message }}
                </div>
                {% endif %}

                {% if predictions|length > 1 %}
                <hr>
                <h6 class="text-muted">其他可能的结果:</h6>
                <ul class="list-group list-group-flush">
                    {% for prediction in predictions[1:] %}
                    <li class="list-group-item">
                        <div class="d-flex w-100 justify-content-between">
                            <h6 class="mb-1">{{ prediction.class_name }}</h6>
                            <small>{{ "%.2f"|format(prediction.probability * 100) }}%</small>
                        </div>
                        <div class="progress mb-2" style="height: 15px;">
                            <div class="progress-bar bg-info" role="progressbar" style="width: {{ prediction.probability * 100 }}%;" aria-valuenow="{{ prediction.probability * 100 }}" aria-valuemin="0" aria-valuemax="100"></div>
                        </div>
                    </li>
                    {% endfor %}
                </ul>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endif %}
<script>
    document.getElementById('file').addEventListener('change', function(event) {
        const file = event.target.files[0];
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('image-preview').src = e.target.result;
            document.getElementById('file-info').innerText = `文件名: ${file.name}, 大小: ${(file.size / 1024).toFixed(2)} KB`;
        };
        reader.readAsDataURL(file);
    });

// 表单验证
(function () {
    'use strict'
    var forms = document.querySelectorAll('.needs-validation')
    Array.prototype.slice.call(forms).forEach(function (form) {
        form.addEventListener('submit', function (event) {
            if (!form.checkValidity()) {
                event.preventDefault()
                event.stopPropagation()
            }
            form.classList.add('was-validated')
        }, false)
    })
})()
</script>
{% endblock %}
